import { api } from '@/global/utils/api';
import { useI18n } from 'vue-i18n';
import { Modal } from 'ant-design-vue';
import JustAInput from '@components/public/JustAInput.vue';
import CreateStaffComponent from '@components/public/CreateStaffComponent.vue';
import { createVNode,ref, reactive } from 'vue';
import useApiPromiseHandler from './api-promise-handler';


export default function useStaffAccountModifier(onSuccessed, onFaild) {
  const { t } = useI18n();

  const openChangeAccountPasswordModal = (accountId) => {
    const modal = Modal.confirm({
      title: "请输入新密码",
      content: createVNode(JustAInput, {onOk: (newPassword) => {
        modal.destroy();
        useApiPromiseHandler(t,
          api.get(`/account/modifyAccountPassword/${accountId}/${newPassword}`),
          onSuccessed, onFaild
        )
      }, onCancel: () => {
        modal.destroy();
      }}),
      cancelButtonProps: {style: {display: 'none'}},
      okButtonProps: {style: {display: 'none'}},
    })
  }

  const post = (url, staff) => {
    useApiPromiseHandler(t,
      api.post(url, staff),
      onSuccessed, onFaild
    );
  }

  const changeStaffAccount = (staff) => {
    post('/staff/modifyStaffAccount', staff);
  }


  const deleteStaff = (staff) => {
    post('/staff/deleteStaff', staff);
  }

  const openCreateStaffModal = () => {
    const createdStaff = reactive({
      name: '',
      gender: 'MALE',
      email: '',
      phone: '',
      account: {
        username: '',
        password: '',
      },
      department: {
        id: 1,
      }
    });
    Modal.confirm({
      title: t('operations.createStaff'),
      content: createVNode(CreateStaffComponent, {staff: createdStaff, t}),
      onOk: () => {
        useApiPromiseHandler(t,
          api.post('/staff/createStaff', createdStaff),
          onSuccessed,
          onFaild
        )
      }
    })
  }

  return { openChangeAccountPasswordModal, openCreateStaffModal, changeStaffAccount, deleteStaff };
}